package com.kang.direct;

import com.kang.utils.RabbitMQUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Customer1 {

    public static void main(String[] args) throws IOException, TimeoutException {
        //通过工具类获取连接对象
        Connection connection = RabbitMQUtils.getConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //通道绑定对应的消息队列
        //参数1：队列名称
        //参数2：消息类型
        channel.exchangeDeclare("logs_direct", "direct");
        //临时队列
        String queue = channel.queueDeclare().getQueue();
        //绑定交换机和队列
        //参数1：队列名称  参数2：交换机名称   参数3：路由键名称
        channel.queueBind(queue, "logs_direct", "error");
        //消费消息
        channel.basicConsume(queue, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者1：" + new String(body));
            }
        });
    }
}
